package com.jhsw.roadsun.module.business.controller.admin.expressage;

import com.jhsw.roadsun.framework.apilog.core.annotation.ApiAccessLog;
import com.jhsw.roadsun.framework.common.pojo.CommonResult;
import com.jhsw.roadsun.framework.common.pojo.PageParam;
import com.jhsw.roadsun.framework.common.pojo.PageResult;
import com.jhsw.roadsun.framework.idempotent.core.annotation.Idempotent;
import com.jhsw.roadsun.framework.idempotent.core.keyresolver.impl.UserIdempotentKeyResolver;
import com.jhsw.roadsun.framework.security.core.util.SecurityFrameworkUtils;
import com.jhsw.roadsun.module.business.controller.admin.expressage.vo.*;
import com.jhsw.roadsun.module.business.service.expressage.ExpressageService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static com.jhsw.roadsun.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static com.jhsw.roadsun.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 寄件信息")
@RestController
@RequestMapping("/business/expressage")
@Validated
public class ExpressageController {

    @Resource
    private ExpressageService expressageService;

    @PostMapping("/create")
    @Operation(summary = "创建寄件信息")
    @PreAuthorize("@ss.hasPermission('business:expressage:create')")
    public CommonResult<Long> createExpressage(@Valid @RequestBody ExpressageSaveReqVO createReqVO) {
        return success(expressageService.createExpressage(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新寄件信息")
    @PreAuthorize("@ss.hasPermission('business:expressage:update')")
    public CommonResult<Boolean> updateExpressage(@Valid @RequestBody ExpressageSaveReqVO updateReqVO) {
        expressageService.updateExpressage(updateReqVO, SecurityFrameworkUtils.getLoginUserId());
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除寄件信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('business:expressage:delete')")
    public CommonResult<Boolean> deleteExpressage(@RequestParam("id") Long id) {
        expressageService.deleteExpressage(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除寄件信息")
    @PreAuthorize("@ss.hasPermission('business:expressage:delete')")
    public CommonResult<Boolean> deleteExpressageList(@RequestParam("ids") List<Long> ids) {
        expressageService.deleteExpressageListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得寄件信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('business:expressage:query')")
    public CommonResult<ExpressageDetailRespVO> getExpressage(@RequestParam("id") Long id) {
        return success(expressageService.getExpressage(id));
    }

    @GetMapping("/page")
    @Operation(summary = "获得寄件信息分页")
    @PreAuthorize("@ss.hasPermission('business:expressage:query')")
    public CommonResult<PageResult<ExpressageRespVO>> getExpressagePage(@Valid ExpressagePageReqVO pageReqVO) {
        return success(expressageService.getExpressagePage(pageReqVO));
    }

    @PostMapping("/download/{type}")
    @Operation(summary = "下载文件")
    @PreAuthorize("@ss.hasPermission('business:expressage:export')")
    @Idempotent(timeout = 3, message = "您的手速太快啦！", keyResolver = UserIdempotentKeyResolver.class)
    public CommonResult<String> downloadFile(@PathVariable("type") Integer type, @RequestBody @Valid ExpressageDownloadReqVO downloadReqVO) {
        return success(expressageService.downloadFile(type, downloadReqVO.getIds()));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出寄件信息 Excel")
    @PreAuthorize("@ss.hasPermission('business:expressage:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportExpressageExcel(@Valid ExpressagePageReqVO pageReqVO,
                                      HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        expressageService.exportExpressageExcel(pageReqVO, response);
    }
}